package com.devilvan.betrice.houseprice.controller;

import com.devilvan.betrice.exception.BetriceException;
import com.devilvan.betrice.exception.constant.BetriceExceptionConstant;
import com.devilvan.betrice.houseprice.constant.HousePriceRedisKeyConstant;
import com.devilvan.betrice.houseprice.service.HousePriceService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.repository.query.Param;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author Evad.Wu
 * @Description 房价信息 控制类
 * @date 2022-07-11
 */
@Slf4j
@RestController
@RequestMapping(value = "/housePriceController")
@Tag(name = "房价信息 控制器")
@SuppressWarnings(value = "unused")
public class HousePriceController {
    @Resource(name = "housePriceServiceImpl")
    private HousePriceService housePriceService;

    /**
     * 定时任务：请求房价信息
     */
    @Async(value = "threadPoolExecutor")
    @XxlJob(value = "doFangke")
    @Operation(summary = "定时任务：请求房价信息")
    @GetMapping(value = "/doFangke")
    public void doFangke() {
        // 获取定时器参数
        String jobParam = Optional.ofNullable(XxlJobHelper.getJobParam())
                .orElseThrow(() -> new BetriceException(BetriceExceptionConstant.JOB_PARAMS_NOT_NULL_EXP.getMessage()));
        HousePriceRedisKeyConstant constant = Optional.of(HousePriceRedisKeyConstant.valueOf(jobParam))
                .orElseThrow(() -> new BetriceException(BetriceExceptionConstant.ENUMERATION_NOT_EXISTS_EXP.getMessage()));
        housePriceService.doFangke(constant);
    }

    /**
     * 独立请求：
     *
     * @param cityName 城市名字，获得枚举对象
     */
    @Operation(summary = "独立请求")
    @GetMapping(value = "/doFangkeParam")
    public void doFangke(@Param("cityName") String cityName) {
        HousePriceRedisKeyConstant constant = Optional.of(HousePriceRedisKeyConstant.valueOf(cityName))
                .orElseThrow(() -> new BetriceException(BetriceExceptionConstant.ENUMERATION_NOT_EXISTS_EXP.getMessage()));
        housePriceService.doFangke(constant);
    }
}
